package com.gopay.webtest.controller.pressureTest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.util.ApiUtil;
import com.gopay.webtest.vo.pressureTest.ProssureTestVo;
@Controller
@RequestMapping("/pressureTest")
public class PressureTestController extends BaseController {
    @RequestMapping("/signApiPressureTestIndex")
    public ModelAndView showSignPressureTestPage(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("gopay.webtest.signPageIndex");
        return mav;
    }
    @RequestMapping("/signApiPressureTest")
    public ModelAndView signPressureTest(String signContext,String privateKey,String limitMills,String concurrentNum){
        ModelAndView mav = new ModelAndView();
        final int concurrentLimits;
        if(StringUtils.isNotBlank(concurrentNum)){
            concurrentLimits = Integer.parseInt(concurrentNum);
        }else{
            concurrentLimits = 1;
        }
        final long limitTime;
        if(StringUtils.isNotBlank(limitMills)){
            limitTime = Long.parseLong(limitMills)*1000;
        }else{
            limitTime = 1000;
        }
        final String context;
        if(StringUtils.isNotBlank(signContext)){
            context = signContext;
        }else{
            context = "您好时间的法拉盛地方了空间啊是";
        }
        ExecutorService pool = Executors.newCachedThreadPool();
        int count =0;
        boolean flag = true;
        long startMills=System.currentTimeMillis();
        final String [] signValue = new String[concurrentLimits];
        final String privateKeyVal;         
        if(StringUtils.isNotBlank(privateKey)){
            privateKeyVal = privateKey;
        }else{
            privateKeyVal ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCuof4rrGxiiE0Tm1+1aKHGacKOl1qdBVY9BVHcei8zy75Pg5YuvHH58qySj8ueqWv9q4kV69ZrEMAA66WxWkyKu/NUKUHIyIAFwwCPMHC3Hzj8mIxrg8ytvfg7pQfNQCZgGAntfwR8I90V++6fyue80GmIZNdkRrC3UX8QBwKhpu/zv1KxURjXYIcLOd/cLH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tXaZUVOuInCBb02rWlPaSSuzB1I78dHL5jqEH/ZbEcmwz3mfnL3WWlPNAoifbvqSzZBsSTsWVxaV/KIF4/TtIjm2nbhu2RNcbrhgf6bjaXl90j3JWsjwYrgHInGhINWzAoGBALycJarMDmrBTix1GoKMU9mZczm0rKVfAZWgyJSigViGg4BAYW8kn5c1AJErowSNgBGcXZdRl3L77Xry7Er0BuxXdhi+4t8F85GVNXegJUTPPnLtnbA4t1YemYLG+yL12eI2BvJa4S0r+JcT2raOfk5gDmT/NrwoohYYthrr565j";
        }
        while(flag){
            for(int i = 0;i<concurrentLimits;i++){
                pool.execute(new Runnable(){
                    public void run(){
                        signValue[0] = ApiUtil.sign(context, privateKeyVal);
                    }
                });
            }   
            long endMills=System.currentTimeMillis();
            if(endMills-startMills>limitTime){
                flag = false;
            }else{
                count++;
            }
        }
        pool.shutdown();
        int perLimitTime;
        if(StringUtils.isNotBlank(limitMills)){            
            perLimitTime = Integer.parseInt(limitMills);
        }else{
            perLimitTime = 1;
        }
        int preCount = count / perLimitTime;
        ProssureTestVo vo = new ProssureTestVo();
        String limitCount = ""+count;
        String preCountStr = ""+preCount;
        vo.setLimitCount(limitCount);
        vo.setPreCount(preCountStr);
        vo.setSignValue(signValue[0]);
        mav.addObject("vo",vo);
        mav.setViewName("gopay.webtest.signPageResult");
        return mav;
    }
    
    @RequestMapping("/checkApiPressureTestIndex")
    public ModelAndView showScheckApiPressureTestPage(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("gopay.webtest.checkApiPressureTestIndex");
        return mav;
    }
    
    @RequestMapping("/checkApiPressureTest")
    public ModelAndView checkApiPressureTest(String context,String publicKey,String signValue,String limitMills,String concurrentNum){
        System.out.println(context);
        System.out.println(publicKey);
        System.out.println(signValue);
        System.out.println(limitMills);
        System.out.println(concurrentNum);
        
        ModelAndView mav = new ModelAndView();
        final int concurrentLimits;
        if(StringUtils.isNotBlank(concurrentNum)){
            concurrentLimits = Integer.parseInt(concurrentNum);
        }else{
            concurrentLimits = 1;
        }
        final long limitTime;
        if(StringUtils.isNotBlank(limitMills)){
            limitTime = Long.parseLong(limitMills)*1000;
        }else{
            limitTime = 1000;
        }
        final String signContext;
        if(StringUtils.isNotBlank(context)){
            signContext = context;
        }else{
            signContext = "您好时间的法拉盛地方了空间啊是";
        }
        final String signVal;
        if(StringUtils.isBlank(signValue)){
            signVal = "eQRvyN0j6EW0j0CKlZ3gYnPoO5F4ZXiAU4yZtEUXupDGoDJbHaeBmJbH0EVnQUaJc57UsjktyF916toEdw04HaVYmgBZTigP46ty1NTgdtkLH9C51/d7j1JltIRp7Yu0VCRNujvMXpdXRgtF9cAHn3RzEBQakJaARJJZR6p+18MLsPt2v9wICSuptACRlIFoXhnQbf95lGHZCWIF+rLYdSjxNTT/teSinftFjAOV25D2r8ACormeJ9VjY5UpwcSHhLSLWLxHS7/2d/8+kbRITrG3/Mn49nRxt0UOJogNRsfQqWm5HZ5LKRlUUn2+oPQGi/idSKTO5mAD2TcuWg5L1Q==";
        }else{
            signVal = signValue;
        }
        final String publicKeyStr;
        if(StringUtils.isBlank(publicKey)){
            publicKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnuiRBZWrWjnTkHN0lrPDpYmERvx344AbJi/t/mjiVqmsl/41qAXjijJ3cL6uWjEdPUF6bIvIj5FwyWzo2GBp+9eRQKGSukR1ARbciJKP0sz7S0xr03B9azfMTYeR6trhsja9wKJCMa512hfdB1VImwmN8/jBo8M8fPfWWByEeivq47eqifsbUyizDAoIlWmw5yyGKwkpEosODmkWbl5RphHMqpj9uti50g1d6dNbqzUEFtDA2XD6XRfdukG+mGhhRraaRFgPV0q7xMLIqmNSIUxgJgCi1m07Tscc0rJZWnKwXpYEhHJoVSkXfFuUn26DQ4LoU/Sz4IcH3FGgnUvZJwIDAQAB";
        }else{
            publicKeyStr = publicKey;
        }
        ExecutorService pool = Executors.newCachedThreadPool();
        int count =0;
        boolean flag = true;
        long startMills=System.currentTimeMillis();
       
        while(flag){
            for(int i = 0;i<concurrentLimits;i++){
                pool.execute(new Runnable(){
                    public void run(){
                        ApiUtil.doCheckByPressureTest(signContext, signVal, publicKeyStr);
                    }
                });
            }   
            long endMills=System.currentTimeMillis();
            if(endMills-startMills>limitTime){
                flag = false;
            }else{
                count++;
            }
        }
        pool.shutdown();
        int perLimitTime = Integer.parseInt(limitMills);
        int preCount = count / perLimitTime;
        ProssureTestVo vo = new ProssureTestVo();
        String limitCount = ""+count;
        String preCountStr = ""+preCount;
        vo.setLimitCount(limitCount);
        vo.setPreCount(preCountStr);
        mav.addObject("vo",vo);
        mav.setViewName("gopay.webtest.checkApiResult");
        return mav;
    }
}
